﻿using RuTracker.Interface;
using RuTracker.Interface.Editors;
using RuTrackerLib.ServiceAttibutes;
using RuTrackerLib.Tracker.RequestProcessors;

namespace RuTrackerLib.Tracker
{
    public interface IRequestProcessor
    {
        RequestProcessingResults ProcessRequest(string query, string user);
    }

    [PerCallService]
    public class RequestProcessor : IRequestProcessor
    {
        private readonly IResponseLexer _lexer;
        private readonly ITrackerAccessor _trackerAccessor;
        private readonly IUserRepository _userRepository;
        private readonly ISessionStorage _sessionStorage;
        private readonly IHtmlEditor[] _editors;

        public RequestProcessor(IResponseLexer lexer
            , ITrackerAccessor trackerAccessor
            , IUserRepository userRepository
            , ISessionStorage sessionStorage
            , IHtmlEditor[] editors)
        {
            _editors = editors;
            _sessionStorage = sessionStorage;
            _lexer = lexer;
            _trackerAccessor = trackerAccessor;
            _userRepository = userRepository;
        }

        public RequestProcessingResults ProcessRequest(string query, string user)
        {
            IRequestProcessor processor = GetProcessor(query);
            return processor.ProcessRequest(query, user);
        }


        private IRequestProcessor GetProcessor(string query)
        {
            if (query.StartsWith(SearchRequestProcessor.SearchTag))
                return new SearchRequestProcessor(_trackerAccessor, _userRepository, _sessionStorage, _editors);

            return new LinkRequestProcessor(_lexer, _trackerAccessor, _userRepository, _sessionStorage, _editors);
        }

    }
}